function AddEditVehicleWindow(navGroup, mode) {
	var isInEditMode = false;
	if(mode == 1) {
		isInEditMode = true;
	}
	
	var self = Ti.UI.createWindow({
		title:(isInEditMode == true ? 'Edit Vehicle' : 'Add New Vehicle'),
		barColor:'#3F4C6B'
	});
	var photoWindow = Ti.UI.createWindow({
		title:'Take Photo',
		barColor:'#3F4C6B'
	});
	
	var photoView = Ti.UI.createView({
		backgroundColor:'black'
	});
	
	photoWindow.add(photoView);
	
	// Event handlers
	self.addEventListener('wantToTakeVehiclePhotoEvent', function(e) {
		// We show an option menu.
		var photoOptions = ['Take Photo', 'Choose Existing', 'Cancel'];
		var photoOptionsDialog = Titanium.UI.createOptionDialog({
	    	options: photoOptions,
	    	cancel:2
		});
		photoOptionsDialog.addEventListener('click', function(photoOptionEvent) {
			if(photoOptionEvent.index != photoOptionEvent.cancel) {
				if(photoOptionEvent.index == 0) {
					showCamera();	
				} else if (photoOptionEvent.index == 1) {
					showPhotoGallery();
				}
			}
		});
	
		photoOptionsDialog.show();
		
		function showCamera() {
			Titanium.Media.showCamera({
			
				success:function(event)
				{
					var cropRect = event.cropRect;
					var image = event.media;
					var div = cropRect.width / 160;
					
					if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
					{
						var views = self.getChildren();
						
						for(var i=0;i<views.length;i++) {
							views[i].fireEvent('gotVehiclePhotoEvent', {
								thumbnailData: event.media
									.imageAsResized(cropRect.width/div, cropRect.height/div)
									.imageWithRoundedCorner(3, 0),
								width: (cropRect.width/div)/2,
								height: (cropRect.height/div)/2
							});
						}
					}
					else
					{
						alert("Got the wrong media type back; " + event.mediaType);
					}
				},
				cancel:function() {},
				error:function(error)
				{
					// create alert
					var a = Titanium.UI.createAlertDialog({title:'Camera'});
			
					// set message
					if (error.code == Titanium.Media.NO_CAMERA)
					{
						a.setMessage('Camera not found');
					}
					else
					{
						a.setMessage('Unexpected error: ' + error.code);
					}
			
					// show alert
					a.show();
				},
				saveToPhotoGallery:false,
				allowEditing:true,
				mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
			});
		}
	
		function showPhotoGallery() {
			Titanium.Media.openPhotoGallery({
			
				success:function(event)
				{
					var cropRect = event.cropRect;
					var image = event.media;
					var div = cropRect.width / 160;
					
					if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
					{
						var views = self.getChildren();
						
						for(var i=0;i<views.length;i++) {
							views[i].fireEvent('gotVehiclePhotoEvent', {
								thumbnailData: event.media
									.imageAsResized(cropRect.width/div, cropRect.height/div)
									.imageWithRoundedCorner(3, 0),
								width: (cropRect.width/div)/2,
								height: (cropRect.height/div)/2
							});
						}
					}
					else
					{
						alert("Got the wrong media type back; " + event.mediaType);
					}
				},
				cancel:function()
				{
			
				},
				error:function(error)
				{
				},
				allowEditing:true,
				mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
			});
		}
	});

	self.addEventListener('saveVehicleEvent', function(e) {
		
		var Vehicle = require('models/Vehicle');
		Vehicle.save(
			e.make,
			e.model,
			e.fuelType,
			e.notes,
			e.photo,
			e.isWidePhoto
		);
		
		navGroup.close(self);
	});
	
	self.addEventListener('updateVehicleEvent', function(e) {
		
		var Vehicle = require('models/Vehicle');
		
		Vehicle.update(
			e.vehicleId,
			e.make,
			e.model,
			e.fuelType,
			e.notes,
			e.photo,
			e.isWidePhoto
		);
		
		navGroup.close(self);
	});
	
	return self;
}

module.exports = AddEditVehicleWindow;